package com.ftg.youx.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.youx.dto.OrdDto;
import com.ftg.youx.entity.OrdPurchaseDetail;
import com.ftg.youx.entity.OrdPurchaseOrder;
import com.ftg.youx.mapper.OrdPurchaseDetailMapper;
import com.ftg.youx.mapper.OrdPurchaseOrderMapper;
import com.ftg.youx.service.IOrdPurchaseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.youx.tools.R;
import com.ftg.youx.vo.PurchaseListVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 进货订单表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2024-10-25
 */
@Service
@Slf4j
public class OrdPurchaseOrderServiceImpl extends ServiceImpl<OrdPurchaseOrderMapper, OrdPurchaseOrder> implements IOrdPurchaseOrderService {

    @Resource
    OrdPurchaseDetailMapper detailMapper;

    @Resource
    RestTemplate restTemplate;

    @Override
    public R<IPage<PurchaseListVo>> purchasePage(Integer page, Integer pageSize) {
        IPage<PurchaseListVo> ipage = new Page<>(page, pageSize);
        ipage.setRecords(this.getBaseMapper().pageForList((page - 1) * pageSize, pageSize));
        ipage.setTotal(this.getBaseMapper().pageCount());
        return R.ok(ipage);
    }

    /**
     * Transactional 事务 :springboot 内置的事务
     *
     * @param order
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public R<Boolean> saveOrder(OrdDto order) {
        log.info("订单生成功能---> 开始");

        OrdPurchaseOrder order1 = new OrdPurchaseOrder();
        order1.setOrderNo(order.getOrderNo());
        order1.setTotalPrice(order.getTotalPrice());
        order1.setInfo(order.getInfo());
        order1.setCreateTime(LocalDateTime.now());
        order1.setEmpId(order.getEmpId());
        this.baseMapper.insert(order1);
        log.info("订单生成已完成");

        //主外键关系
        QueryWrapper<OrdPurchaseOrder> query = new QueryWrapper<>();
        query.eq("order_no",order1.getOrderNo());
        OrdPurchaseOrder ordPurchaseOrder = this.getBaseMapper().selectOne(query);
        //商品id
        List<OrdPurchaseDetail> details = order.getDetails();
        for (int i = 0; i < details.size(); i++) {
            details.get(i).setItemId(details.get(i).getId());
            details.get(i).setCreateTime(LocalDateTime.now());
            details.get(i).setUpdateTime(LocalDateTime.now());
            details.get(i).setPurchaseDetailId(ordPurchaseOrder.getId());
            details.get(i).setId(null);
        }
        //批量保存
        detailMapper.insert(order.getDetails());

        return R.ok(true);
    }

    private String url = "http://localhost:8081/proItem/list";

    @Override
    public String getProInfo(String name, Integer page, Integer pageSize) {
        url += "?page=" + page + "&pageSize=" + pageSize;
        if (name != null && !name.isEmpty()) {
            url += "&name=" + name;
        }
        return restTemplate.getForObject(url, String.class);
    }

    private String empUrl = "http://localhost:8082/sys/emp/getOrderEmpId";

    @Override
    public String getEmpIdInfo() {
        return restTemplate.getForObject(empUrl, String.class);
    }

    //创建订单的时候，我们还会生成一个，创建者吧...


}
